package com.spic.business.accident.interevents.eviewreport.service.impl;

import com.spic.business.accident.analysisReport.domain.AccidentAnalysisReport;
import com.spic.business.accident.analysisReport.mapper.AccidentAnalysisReportMapper;
import com.spic.business.accident.interevents.eviewreport.domain.InternalEventReviewReport;
import com.spic.business.accident.interevents.eviewreport.domain.ReviewDto;
import com.spic.business.accident.interevents.eviewreport.mapper.InternalEventReviewReportMapper;
import com.spic.business.accident.interevents.eviewreport.service.IInternalEventReviewReportService;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.security.utils.cache.DeptCacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 内部事件复查报告Service业务层处理
 *
 * @author 任翔
 * @date 2021-12-01
 */
@Service
public class InternalEventReviewReportServiceImpl implements IInternalEventReviewReportService
{
    @Autowired
    private InternalEventReviewReportMapper internalEventReviewReportMapper;
    @Autowired
    private AccidentAnalysisReportMapper accidentAnalysisReportMapper;

    /**
     * 查询内部事件复查报告
     *
     * @param id 内部事件复查报告主键
     * @return 内部事件复查报告
     */
    @Override
    public InternalEventReviewReport selectInternalEventReviewReportById(Long id)
    {
        InternalEventReviewReport internalEventReviewReport = internalEventReviewReportMapper.selectInternalEventReviewReportById(id);
        try {
            if (StringUtils.isNotEmpty(internalEventReviewReport.getReviewDept())){
                String reviewDept = DeptCacheUtils.getValue(Long.parseLong(internalEventReviewReport.getReviewDept()));
                internalEventReviewReport.setReviewDept(reviewDept);
            }
            if (StringUtils.isNotEmpty(internalEventReviewReport.getAccidentDepartment())){
                String dutyDept = DeptCacheUtils.getValue(Long.parseLong(internalEventReviewReport.getAccidentDepartment()));
                internalEventReviewReport.setDutyDept(dutyDept);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return internalEventReviewReport;
    }

    /**
     * 查询内部事件复查报告列表
     *
     * @param internalEventReviewReport 内部事件复查报告
     * @return 内部事件复查报告
     */
    @Override
    @BusinessDataScope(menuPath = "business/accident/interEvent/eviewReport/index")
    public List<InternalEventReviewReport> selectInternalEventReviewReportList(InternalEventReviewReport internalEventReviewReport)
    {
        return internalEventReviewReportMapper.selectInternalEventReviewReportList(internalEventReviewReport);
    }

    /**
     * 新增内部事件复查报告
     *
     * @param internalEventReviewReport 内部事件复查报告
     * @return 结果
     */
    @Override
    public int insertInternalEventReviewReport(InternalEventReviewReport internalEventReviewReport)
    {
        //更新事故分析报告
        if (StringUtils.isNotEmpty(internalEventReviewReport.getAccidentType())){
            AccidentAnalysisReport report = new AccidentAnalysisReport();
            report.setId(internalEventReviewReport.getAccidentAnalysisReportId());
            report.setAccdientType(internalEventReviewReport.getAccidentType());
            accidentAnalysisReportMapper.updateById(report);
        }
        internalEventReviewReport.setCreateTime(DateUtils.getNowDate());
        return internalEventReviewReportMapper.insertInternalEventReviewReport(internalEventReviewReport);
    }

    /**
     * 修改内部事件复查报告
     *
     * @param internalEventReviewReport 内部事件复查报告
     * @return 结果
     */
    @Override
    public int updateInternalEventReviewReport(InternalEventReviewReport internalEventReviewReport)
    {
        internalEventReviewReport.setUpdateTime(DateUtils.getNowDate());
        return internalEventReviewReportMapper.updateInternalEventReviewReport(internalEventReviewReport);
    }

    /**
     * 批量删除内部事件复查报告
     *
     * @param ids 需要删除的内部事件复查报告主键
     * @return 结果
     */
    @Override
    public int deleteInternalEventReviewReportByIds(Long[] ids)
    {
        return internalEventReviewReportMapper.deleteInternalEventReviewReportByIds(ids);
    }

    /**
     * 删除内部事件复查报告信息
     *
     * @param id 内部事件复查报告主键
     * @return 结果
     */
    @Override
    public int deleteInternalEventReviewReportById(Long id)
    {
        return internalEventReviewReportMapper.deleteInternalEventReviewReportById(id);
    }

    @Override
    public List<ReviewDto> reviewList(Map params)
    {
        return internalEventReviewReportMapper.reviewList(params);
    }
}
